package com.rokyinfo.pdm.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.rokyinfo.generator.*;
import com.rokyinfo.pdm.entity.*;
import com.rokyinfo.pdm.mapper.DDistributionRecordMapper;
import com.rokyinfo.pdm.service.IDDistributionRecordService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 设备生成记录表 服务实现类
 * </p>
 *
 * @author YuanZhiJian
 * @since 2017-01-22
 */

@Service

public class DDistributionRecordServiceImpl extends ServiceImpl<DDistributionRecordMapper, DDistributionRecord> implements IDDistributionRecordService {

    @Value("${com.rokyinfo.pdm.generator.lastsn}")
    private String lastSn;

    @Value("${com.rokyinfo.pdm.generator.lastimei}")
    private String lastImei;

    @Value("${com.rokyinfo.pdm.generator.lastbtaddress}")
    private String lastBtAddress;

    @Value("${com.rokyinfo.pdm.generator.lastbleaddress}")
    private String lastBleAddress;

    @Value("${com.rokyinfo.pdm.generator.lastordernum}")
    private String lastOrderNum;

    public Page<DDistributionRecord> selectDistributionRecordList(Page<DDistributionRecord> page) {
        page.setOrderByField("generator_id");
        page.setAsc(false);
        page.setRecords(baseMapper.selectDistributionRecordList(page));
        return page;
    }

    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public Long generateDevices(Integer quantity, Long modelId, Long firmId, Long creatorId) throws Exception {
        DModel model = new DModel();
        model.setModelId(modelId);
        model = model.selectById();
        if (model == null) {
            throw new Exception("设备型号不存在");
        }

        DDistributionRecord mDDistributionRecord = new DDistributionRecord();

        String generatorNum = RecordNumGenerator.generateOrderNum(model.getName(), lastOrderNum, quantity);
        mDDistributionRecord.setCreateTime(new Date());
        mDDistributionRecord.setGeneratorNum(generatorNum);
        mDDistributionRecord.setModelId(modelId);
        mDDistributionRecord.setFirmId(firmId);
        mDDistributionRecord.setCreateBy(creatorId);
        mDDistributionRecord.setQuantity(quantity);

        baseMapper.insert(mDDistributionRecord);
        Date createTime = new Date();

        DDevice dDevice = new DDevice();
        DBluetoothInfo dBluetoothInfo = new DBluetoothInfo();
        DRadioInfo dRadioInfo = new DRadioInfo();

        for (int i = 0; i < quantity; i++) {

            dDevice.setId(null);
            dDevice.setModelId(modelId);
            dDevice.setFirmId(firmId);
            dDevice.setCreateTime(createTime);
            dDevice.setCreateBy(creatorId);
            dDevice.setGeneratorId(mDDistributionRecord.getGeneratorId());

            //扩展信息处理
            if (model.haveBluetoothSpeciality()) {

                String sn = SNGenerator.generateUESn(SNGenerator.UEType.CCU, lastSn, i);

                dDevice.setSn(sn);
                dDevice.insert();

                String btAddress = MacAddressGenerator.generateMacAddr(lastBtAddress, i);
                String bleAddress = MacAddressGenerator.generateMacAddr(lastBleAddress, i);

                dBluetoothInfo.setId(null);
                dBluetoothInfo.setSn(sn);
                dBluetoothInfo.setMacAddress0(btAddress);
                dBluetoothInfo.setMacAddress1(bleAddress);
                dBluetoothInfo.setEncryptKey(AuthKeyGenerator.getRandomString(24));
                dBluetoothInfo.insert();

            } else if (model.haveRadioSpeciality()) {

                String sn = SNGenerator.generateUESn(SNGenerator.UEType.DCU, lastSn, i);

                dDevice.setSn(sn);
                dDevice.insert();

                String imei = IMEIGenerator.generateIMEI(lastImei, i);

                dRadioInfo.setId(null);
                dRadioInfo.setSn(sn);
                dRadioInfo.setImei(imei);
                dRadioInfo.insert();

            }
        }

        return mDDistributionRecord.getGeneratorId();

    }

}
